package com.runa.monitor.platinterface.mqtt.vo;

import com.alibaba.fastjson.JSON;
import com.runa.persistence.DateUtil;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;

@Slf4j
@Data
public class FourGValveVO {
    private String id;
    private String rxw;
    private String nbdly;
    private String nbres;
    private String csq;
    private String icc;
    private String imei;
    private String gps;
    private String rsrp;
    private String rsrq;
    private String snr;
    private String rssi;
    private String set;
    private String cur;
    private String rcnt;
    private String t1;
    private String t2;
    private String sta;
    private String par;
    private String ver;
    private String mode;
    private String tac;
    private String rtc;
    private String citv1;
    private String uitv1;
    private String citv2;
    private String uitv2;
    private String hot;
    private String bat;
    private String life;
    private String t1c;
    private String t2c;
    private String d1k;
    private String acls;
    private String aclt;
    private String block;
    private String motos;
    private String reset;

    private String mok;
    private String mdt;
    private String mjrl;
    private String mrl;
    private String mll;
    private String mrgl;
    private String mlsl;
    private String mt1;
    private String mt2;
    private String mst;

    public static void main(String[] args) {
        String s = "func:up,id:87654321,typ:3,nbattach1:15,nbr:8,nbdelay:2,rep:229,rrem:300,csq:0,icc: 89861123209093132072\n" +
                ",imei:864145075284354,rsrp:-99,snr:-1,eco:7F,mver:0,simf:0,4gmod: EC801ECNCGR03A08M02\n" +
                ",T1:[24.8,25.2,25.0,24.9,25.0,25.3,25.0,24.9,25.4,25.5,25.5,25.7,25.4,25.3,25.6,25.5,25.4,25.5],etime:20241113132340,sta:01,ver:1017,rtc:20241113132357,sitv1:5,uitv1:360,sitv2:10,uitv2:2880,hot:10010401,bat:3.610,batv:3.300,life:118286,Tc:0.0,Hc:0.0,reset:0,reperrcnt:0,rmc:10,nbi:1,LFDET:0,hrs:6,ahead:120,cenreptime:10,stf:1,stemp:0|23.0,stul:45.0,stdl:5.0,compile:2024-08-16T13-17-48";
        FourGValveVO gene = FourGValveVO.gene(s);
        System.out.println(JSON.toJSONString(gene));
    }

    @SneakyThrows
    public static FourGValveVO gene(String jso) {
        jso = jso.substring(8);
        Class<FourGValveVO> fourGValveVOClass = FourGValveVO.class;
        FourGValveVO fourGValveVO = fourGValveVOClass.newInstance();
        String[] jian = jso.split(",");
        for (String aJian : jian) {
            String regex = ":";
            String[] ziDuan = aJian.split(regex);
            Field item;
            try {
                item = fourGValveVOClass.getDeclaredField(ziDuan[0]);
                item.setAccessible(true);
                if (ziDuan.length > 1)
                    item.set(fourGValveVO, ziDuan[1]);
            } catch (NoSuchFieldException e) {
                log.error("对应文档无此字段，无法解析" + ziDuan[0]);
            }
        }
        try {
            char[] chars = jso.toCharArray();
            int begin = 0;
            boolean f = false;
            for (int i = 0; i < chars.length; i++) {
                if (!f & chars[i] == ',') {
                    begin = i + 1;
                }
                if (chars[i] == '[') {
                    f = true;
                } else if (chars[i] == ']') {
                    String substring = jso.substring(begin, i+1);
                    String[] split = substring.split(":");
                    if (split.length == 2) {
                        try {
                            Field item = fourGValveVOClass.getDeclaredField(split[0]);
                            item.setAccessible(true);
                            item.set(fourGValveVO, split[1]);
                        } catch (NoSuchFieldException e) {
                            log.error("对应文档无此字段，无法解析" + split[0]);
                        }
                    }
                    begin = i + 1;
                }
            }
        } catch (Exception e) {
            log.error("", e);
        }

        return fourGValveVO;
    }

    public static String faultStr(String sta) {
        int st = Integer.parseInt(sta, 16);
        StringBuilder stringBuilder = new StringBuilder();
//        if ((st >> 3 & 1) == 1) {
//            stringBuilder.append("w25 err;");
//        }
        if ((st >> 4 & 1) == 1) {
            stringBuilder.append("电机堵转;");
        }
//        if ((st >> 8 & 1) == 1) {
//            stringBuilder.append("cat1  init ok;");
//        }
//        if ((st >> 10 & 1) == 1) {
//            stringBuilder.append("bie ok;");
//        }
//        if ((st>>11 & 1)==1){
//            stringBuilder.append("电机堵转;");
//        }
//        if ((st >> 14 & 1) == 1) {
//            stringBuilder.append("自清洁中;");
//        }
        if ((st >> 15 & 1) == 1) {
            stringBuilder.append("电机未标定;");
        }
        return stringBuilder.toString();
    }

    public String wakeUpCycle() {
        return DateUtil.heatingPeriod(hot) ? uitv1 : uitv2;
    }

}
